package com.shxs.service.impl;

import java.util.List;

import com.shxs.mapper.NovelTableMapper;
import com.shxs.model.entity.NovelTable;
import com.shxs.model.entity.VoiceRoleTable;
import com.shxs.service.INovelTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;

/**
 * 小说Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-31
 */
@Service
public class NovelTableServiceImpl implements INovelTableService
{
    @Autowired
    private NovelTableMapper novelTableMapper;

    /**
     * 查询小说
     * 
     * @param novelId 小说主键
     * @return 小说
     */
    @Override
    public NovelTable selectNovelTableByNovelId(Long novelId)
    {
        return novelTableMapper.selectNovelTableByNovelId(novelId);
    }

    /**
     * 查询小说列表
     * 
     * @param novelTable 小说
     * @return 小说
     */
    @Override
    public List<NovelTable> selectNovelTableList(NovelTable novelTable)
    {
        return novelTableMapper.selectNovelTableList(novelTable);
    }

    /**
     * 新增小说
     * 
     * @param novelTable 小说
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNovelTable(NovelTable novelTable)
    {
        int rows = novelTableMapper.insertNovelTable(novelTable);
        insertVoiceRoleTable(novelTable);
        return rows;
    }

    /**
     * 修改小说
     * 
     * @param novelTable 小说
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNovelTable(NovelTable novelTable)
    {
        novelTableMapper.deleteVoiceRoleTableByNovelId(novelTable.getNovelId());
        insertVoiceRoleTable(novelTable);
        return novelTableMapper.updateNovelTable(novelTable);
    }

    /**
     * 批量删除小说
     * 
     * @param novelIds 需要删除的小说主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNovelTableByNovelIds(Long[] novelIds)
    {
        novelTableMapper.deleteVoiceRoleTableByNovelIds(novelIds);
        return novelTableMapper.deleteNovelTableByNovelIds(novelIds);
    }

    /**
     * 删除小说信息
     * 
     * @param novelId 小说主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNovelTableByNovelId(Long novelId)
    {
        novelTableMapper.deleteVoiceRoleTableByNovelId(novelId);
        return novelTableMapper.deleteNovelTableByNovelId(novelId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param novelTable 小说对象
     */
    public void insertVoiceRoleTable(NovelTable novelTable)
    {
        List<VoiceRoleTable> voiceRoleTableList = novelTable.getVoiceRoleTableList();
        Long novelId = novelTable.getNovelId();
        if (voiceRoleTableList!= null)
        {
            List<VoiceRoleTable> list = new ArrayList<VoiceRoleTable>();
            for (VoiceRoleTable voiceRoleTable : voiceRoleTableList)
            {
                voiceRoleTable.setNovelId(novelId);
                list.add(voiceRoleTable);
            }
            if (list.size() > 0)
            {
                novelTableMapper.batchVoiceRoleTable(list);
            }
        }
    }
}
